package amani.wechat.platform.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import amani.wechat.platform.controller.model.Result;
import amani.wechat.platform.services.ICommonService;
import amani.wechat.platform.utils.CompressPic;
import amani.wechat.platform.webserviceClient.StaffHairDesignerInfo;
import amani.wechat.platform.webserviceClient.Staffinfo;
import amani.wechat.platform.webserviceClient.imageService.ImageCloudService;
import amani.wechat.platform.webserviceClient.imageService.ImageCloudServicePortType;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/Staff")
public class StaffController {
	
	@Resource(name="CommonService")
	ICommonService commonService ;
	
	static ImageCloudServicePortType aImageCloudServicePortType;
	
	static{
		ImageCloudService  aImageCloudService = new ImageCloudService();
		aImageCloudServicePortType = aImageCloudService.getImageCloudServiceHttpPort();
	}
	/**
	 * 头像 昵称维护界面
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/index")
	public String index(Model model,HttpSession session) {
		String openid = session.getAttribute("openid")+"";
		Staffinfo aStaffinfo = commonService.findStaffinfo(openid);
		model.addAttribute("staff", aStaffinfo);
		return "staff/index";
	}
	/**
	 * 更新头像和昵称
	 * @param image
	 * @param hm
	 * @param session
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/index/upload")
	@ResponseBody
	public Result indexUpload(@RequestParam(value = "image", required = false)MultipartFile image,String hm, HttpSession session)  {
		String imagePath = "";
		Result res = new Result();
		String openid = session.getAttribute("openid")+"";
		res.setIsok(true);
		if(image!=null){
			String fileName = new Date().getTime() + "_" + image.getOriginalFilename();
	        String key = fileName;
	        try {
	        	byte[] img = CompressPic.compressPic(image.getInputStream(), 300, 300);
	        	imagePath = aImageCloudServicePortType.uploadFile(img, "amani", key);
	        	Gson gson = new Gson();
	    		Map<String, String> map = gson.fromJson(imagePath, new TypeToken<Map<String, String>>(){}.getType());
	        	imagePath = map.get("url");
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
        commonService.updateStaffInfo(openid, hm, imagePath);
		return res;
	}
	
	/**
	 * 作品上传 展示
	 * @param id
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/work")
	public String work(Integer id,Model model,HttpSession session) {
		if(id!=null){
			model.addAttribute("work", commonService.findStaffHairDesignerInfo(id));
		}
		return "staff/work";
	}
	/**
	 * 作品上传
	 * @param image
	 * @param id
	 * @param lx
	 * @param bq
	 * @param ms
	 * @param session
	 * @return
	 */
	@RequestMapping("/work/upload")
	@ResponseBody
	public Result workUpload(@RequestParam(value = "image", required = false)MultipartFile image,Integer id,String lx,String bq,String ms, HttpSession session) {
		String imagePath = "";
		Result res = new Result();
		String openid = session.getAttribute("openid")+"";
		res.setIsok(true);
//		openid = "0010101001";
		if(image!=null){
			String fileName = new Date().getTime() + "_" + image.getOriginalFilename();
	        String key = fileName;
	        try {
	        	byte[] img = CompressPic.compressPic(image.getInputStream(), 400, 400);
	        	imagePath = aImageCloudServicePortType.uploadFile(img, "amani", key);
	        	Gson gson = new Gson();
	    		Map<String, String> map = gson.fromJson(imagePath, new TypeToken<Map<String, String>>(){}.getType());
	        	imagePath = map.get("url");
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
        commonService.saveStaffHairDesignerInfo(id, lx, bq, ms, openid, imagePath);
		return res;
	}
	/**
	 * 作品展示 
	 * @param model
	 * @param session
	 * @return
	 */
	@RequestMapping("/works")
	public String works(Model model,HttpSession session) {
		String openid = session.getAttribute("openid")+"";
		List<StaffHairDesignerInfo> works = commonService.loadStaffHairDesignerInfo(openid);
		model.addAttribute("works", works);
		return "staff/works";
	}
	/**
	 * 假期设定
	 * @return
	 */
	@RequestMapping("/holiday")
	public String holiday() {
		return "staff/holiday";
	}
	
	@RequestMapping("/holiday/orderDate")
	@ResponseBody
	public String orderDate(String year ,String month,HttpSession session){
		String openid = session.getAttribute("openid")+"";
		return commonService.getStaffWorkDay(year, month, openid);
	}
	
	@RequestMapping("/holiday/submit")
	@ResponseBody
	public Result submit(String date,HttpSession session){
		Result res = new Result();
		res.setIsok(true);
		String openid = session.getAttribute("openid")+"";
		commonService.saveStaffRestDay(openid, date);
		return res;
	}
	
	/**
	 * 假期设定
	 * @return
	 */
	@RequestMapping("/order")
	public String order() {
		return "staff/order";
	}
	
	@RequestMapping("/order/orderDate")
	@ResponseBody
	public String orderDate2(String year ,String month,HttpSession session){
		String openid = session.getAttribute("openid")+"";
		return commonService.getOrders(year, month, openid);
	}
}
